package fmod.fmod;

import fmod.FMOD_STUDIO_EVENT_PROPERTY;
import fmod.javafmod;
import fmod.javafmodJNI;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.BitSet;
import zombie.GameSounds;
import zombie.SoundManager;
import zombie.audio.BaseSoundEmitter;
import zombie.audio.FMODParameter;
import zombie.audio.GameSound;
import zombie.audio.GameSoundClip;
import zombie.audio.parameters.ParameterOcclusion;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.core.math.PZMath;
import zombie.debug.DebugLog;
import zombie.debug.DebugOptions;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoMovingObject;
import zombie.iso.IsoObject;
import zombie.iso.IsoUtils;
import zombie.iso.IsoWorld;
import zombie.iso.areas.IsoRoom;
import zombie.iso.objects.IsoDoor;
import zombie.iso.objects.IsoWindow;
import zombie.network.GameClient;
import zombie.network.GameServer;
import zombie.popman.ObjectPool;
import zombie.scripting.ScriptManager;
import zombie.scripting.objects.SoundTimelineScript;

/* loaded from: FMODSoundEmitter.class */
public final class FMODSoundEmitter extends BaseSoundEmitter {
    public float x;
    public float y;
    public float z;
    public EmitterType emitterType;
    public IsoObject parent;
    public IFMODParameterUpdater parameterUpdater;
    private static final ObjectPool<ParameterValue> parameterValuePool;
    private static BitSet parameterSet;
    private static long CurrentTimeMS;
    static final /* synthetic */ boolean $assertionsDisabled;
    private final ArrayList<Sound> ToStart = new ArrayList<>();
    private final ArrayList<Sound> Instances = new ArrayList<>();
    private final ParameterOcclusion occlusion = new ParameterOcclusion(this);
    private final ArrayList<FMODParameter> parameters = new ArrayList<>();
    private final ArrayList<ParameterValue> parameterValues = new ArrayList<>();
    private final ArrayDeque<EventSound> eventSoundPool = new ArrayDeque<>();
    private final ArrayDeque<FileSound> fileSoundPool = new ArrayDeque<>();

    static {
        $assertionsDisabled = !FMODSoundEmitter.class.desiredAssertionStatus();
        parameterValuePool = new ObjectPool<>(ParameterValue::new);
        CurrentTimeMS = 0L;
    }

    /* loaded from: FMODSoundEmitter$Sound.class */
    private static abstract class Sound {
        FMODSoundEmitter emitter;
        public GameSoundClip clip;
        public String name;
        public IsoObject parent;
        public float volume = 1.0f;
        public float pitch = 1.0f;
        public float setVolume = 1.0f;
        float setX = 0.0f;
        float setY = 0.0f;
        float setZ = 0.0f;

        abstract long getRef();

        abstract void stop();

        abstract void set3D(boolean z);

        abstract void release();

        abstract boolean tick(boolean z);

        abstract void setParameterValue(FMOD_STUDIO_PARAMETER_DESCRIPTION fmod_studio_parameter_description, float f);

        abstract void setTimelinePosition(String str);

        abstract void triggerCue();

        abstract boolean isTriggeredCue();

        abstract boolean restart();

        Sound(FMODSoundEmitter fMODSoundEmitter) {
            this.emitter = fMODSoundEmitter;
        }

        float getVolume() {
            this.clip = this.clip.checkReloaded();
            return this.volume * this.clip.getEffectiveVolume();
        }
    }

    /* loaded from: FMODSoundEmitter$EventSound.class */
    private static final class EventSound extends Sound {
        long eventInstance;
        boolean bTriggeredCue;
        long checkTimeMS;

        EventSound(FMODSoundEmitter fMODSoundEmitter) {
            super(fMODSoundEmitter);
            this.bTriggeredCue = false;
            this.checkTimeMS = 0L;
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        long getRef() {
            return this.eventInstance;
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        void stop() {
            if (this.eventInstance == 0) {
                return;
            }
            this.emitter.stopEvent(this.eventInstance, this.clip);
            javafmod.FMOD_Studio_EventInstance_Stop(this.eventInstance, false);
            javafmod.FMOD_Studio_ReleaseEventInstance(this.eventInstance);
            this.eventInstance = 0L;
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        void set3D(boolean z) {
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        void release() {
            stop();
            this.checkTimeMS = 0L;
            this.bTriggeredCue = false;
            this.emitter.releaseEventSound(this);
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        boolean tick(boolean z) {
            IsoPlayer.getInstance();
            if (IsoPlayer.numPlayers > 1) {
            }
            if (!z) {
                int FMOD_Studio_GetPlaybackState = javafmod.FMOD_Studio_GetPlaybackState(this.eventInstance);
                if (FMOD_Studio_GetPlaybackState == FMOD_STUDIO_PLAYBACK_STATE.FMOD_STUDIO_PLAYBACK_STOPPING.index) {
                    return false;
                }
                if (FMOD_Studio_GetPlaybackState == FMOD_STUDIO_PLAYBACK_STATE.FMOD_STUDIO_PLAYBACK_STOPPED.index) {
                    javafmod.FMOD_Studio_ReleaseEventInstance(this.eventInstance);
                    this.emitter.stopEvent(this.eventInstance, this.clip);
                    this.eventInstance = 0L;
                    return true;
                }
                if (this.bTriggeredCue && FMODSoundEmitter.CurrentTimeMS - this.checkTimeMS > 250 && FMOD_Studio_GetPlaybackState == FMOD_STUDIO_PLAYBACK_STATE.FMOD_STUDIO_PLAYBACK_SUSTAINING.index) {
                    javafmodJNI.FMOD_Studio_EventInstance_KeyOff(this.eventInstance);
                }
                if (this.bTriggeredCue && this.clip.eventDescription.length > 0 && FMODSoundEmitter.CurrentTimeMS - this.checkTimeMS > 1500) {
                    if (javafmodJNI.FMOD_Studio_GetTimelinePosition(this.eventInstance) > this.clip.eventDescription.length + 1000) {
                        javafmod.FMOD_Studio_EventInstance_Stop(this.eventInstance, false);
                    }
                    this.checkTimeMS = FMODSoundEmitter.CurrentTimeMS;
                }
            }
            if ((Float.compare(this.emitter.x, this.setX) == 0 && Float.compare(this.emitter.y, this.setY) == 0 && Float.compare(this.emitter.z, this.setZ) == 0) ? false : true) {
                this.setX = this.emitter.x;
                this.setY = this.emitter.y;
                this.setZ = this.emitter.z;
                javafmod.FMOD_Studio_EventInstance3D(this.eventInstance, this.emitter.x, this.emitter.y, this.emitter.z * 3.0f);
            }
            float volume = getVolume();
            if (Float.compare(volume, this.setVolume) != 0) {
                this.setVolume = volume;
                javafmod.FMOD_Studio_EventInstance_SetVolume(this.eventInstance, volume);
            }
            if (z) {
                this.emitter.startEvent(this.eventInstance, this.clip);
                javafmod.FMOD_Studio_StartEvent(this.eventInstance);
                return false;
            }
            this.emitter.updateEvent(this.eventInstance, this.clip);
            return false;
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        void setParameterValue(FMOD_STUDIO_PARAMETER_DESCRIPTION fmod_studio_parameter_description, float f) {
            if (this.eventInstance == 0) {
                return;
            }
            javafmod.FMOD_Studio_EventInstance_SetParameterByID(this.eventInstance, fmod_studio_parameter_description.id, f, false);
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        void setTimelinePosition(String str) {
            SoundTimelineScript soundTimeline;
            int position;
            if (this.eventInstance == 0 || this.clip == null || this.clip.event == null || (soundTimeline = ScriptManager.instance.getSoundTimeline(this.clip.event)) == null || (position = soundTimeline.getPosition(str)) == -1) {
                return;
            }
            javafmodJNI.FMOD_Studio_EventInstance_SetTimelinePosition(this.eventInstance, position);
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        void triggerCue() {
            if (this.eventInstance == 0 || !this.clip.hasSustainPoints()) {
                return;
            }
            javafmodJNI.FMOD_Studio_EventInstance_KeyOff(this.eventInstance);
            this.bTriggeredCue = true;
            this.checkTimeMS = FMODSoundEmitter.CurrentTimeMS;
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        boolean isTriggeredCue() {
            return this.bTriggeredCue;
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        boolean restart() {
            if (this.eventInstance == 0) {
                return false;
            }
            javafmodJNI.FMOD_Studio_StartEvent(this.eventInstance);
            return true;
        }
    }

    /* loaded from: FMODSoundEmitter$FileSound.class */
    private static final class FileSound extends Sound {
        long sound;
        long channel;
        byte is3D;
        boolean ambient;
        float lx;
        float ly;
        float lz;

        FileSound(FMODSoundEmitter fMODSoundEmitter) {
            super(fMODSoundEmitter);
            this.is3D = (byte) -1;
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        long getRef() {
            return this.channel;
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        void stop() {
            if (this.channel == 0) {
                return;
            }
            javafmod.FMOD_Channel_Stop(this.channel);
            this.sound = 0L;
            this.channel = 0L;
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        void set3D(boolean z) {
            if (this.is3D != ((byte) (z ? 1 : 0))) {
                javafmod.FMOD_Channel_SetMode(this.channel, z ? FMODManager.FMOD_3D : FMODManager.FMOD_2D);
                if (z) {
                    javafmod.FMOD_Channel_Set3DAttributes(this.channel, this.emitter.x, this.emitter.y, this.emitter.z * 3.0f, 0.0f, 0.0f, 0.0f);
                }
                this.is3D = (byte) (z ? 1 : 0);
            }
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        void release() {
            stop();
            this.emitter.releaseFileSound(this);
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        boolean tick(boolean z) {
            int i;
            int i2;
            int i3;
            IsoGridSquare gridSquare;
            IsoRoom room;
            if (z && this.clip.gameSound.isLooped()) {
                javafmod.FMOD_Channel_SetMode(this.channel, FMODManager.FMOD_LOOP_NORMAL);
            }
            float f = this.clip.distanceMin;
            if (!z && !javafmod.FMOD_Channel_IsPlaying(this.channel)) {
                return true;
            }
            float f2 = this.emitter.x;
            float f3 = this.emitter.y;
            float f4 = this.emitter.z;
            if (!this.clip.gameSound.is3D || (f2 == 0.0f && f3 == 0.0f)) {
                if ((f2 != 0.0f || f3 != 0.0f) && ((z || f2 != this.lx || f3 != this.ly) && this.is3D == 1)) {
                    javafmod.FMOD_Channel_Set3DAttributes(this.channel, f2, f3, f4 * 3.0f, 0.0f, 0.0f, 0.0f);
                }
                javafmod.FMOD_Channel_SetVolume(this.channel, getVolume());
                javafmod.FMOD_Channel_SetPitch(this.channel, this.pitch);
                if (z) {
                    javafmod.FMOD_Channel_SetPaused(this.channel, false);
                    return false;
                }
                return false;
            }
            this.lx = f2;
            this.ly = f3;
            this.lz = f4;
            javafmod.FMOD_Channel_Set3DAttributes(this.channel, f2, f3, f4 * 3.0f, f2 - this.lx, f3 - this.ly, (f4 * 3.0f) - (this.lz * 3.0f));
            float f5 = Float.MAX_VALUE;
            for (int i4 = 0; i4 < IsoPlayer.numPlayers; i4++) {
                IsoPlayer isoPlayer = IsoPlayer.players[i4];
                if (isoPlayer != null && !isoPlayer.isDeaf()) {
                    f5 = PZMath.min(f5, IsoUtils.DistanceTo(f2, f3, f4 * 3.0f, isoPlayer.x, isoPlayer.y, isoPlayer.z * 3.0f));
                }
            }
            javafmodJNI.FMOD_Channel_Set3DLevel(this.channel, f5 >= 2.0f ? 1.0f : 1.0f - ((2.0f - f5) / 2.0f));
            if (IsoPlayer.numPlayers > 1) {
                if (z) {
                    javafmod.FMOD_System_SetReverbDefault(0, FMODManager.FMOD_PRESET_OFF);
                    javafmod.FMOD_Channel_Set3DMinMaxDistance(this.channel, this.clip.distanceMin, this.clip.distanceMax);
                    javafmod.FMOD_Channel_Set3DOcclusion(this.channel, 0.0f, 0.0f);
                }
                javafmod.FMOD_Channel_SetVolume(this.channel, getVolume());
                if (z) {
                    javafmod.FMOD_Channel_SetPaused(this.channel, false);
                }
                javafmod.FMOD_Channel_SetReverbProperties(this.channel, 0, 0.0f);
                javafmod.FMOD_Channel_SetReverbProperties(this.channel, 1, 0.0f);
                javafmod.FMOD_System_SetReverbDefault(1, FMODManager.FMOD_PRESET_OFF);
                javafmod.FMOD_Channel_Set3DOcclusion(this.channel, 0.0f, 0.0f);
                return false;
            }
            float DistanceManhatten = IsoUtils.DistanceManhatten(f2, f3, IsoPlayer.getInstance().x, IsoPlayer.getInstance().y, f4, IsoPlayer.getInstance().z) / this.clip.reverbMaxRange;
            IsoGridSquare currentSquare = IsoPlayer.getInstance().getCurrentSquare();
            if (currentSquare == null) {
                javafmod.FMOD_Channel_Set3DMinMaxDistance(this.channel, f, this.clip.distanceMax);
                javafmod.FMOD_Channel_SetVolume(this.channel, getVolume());
                if (z) {
                    javafmod.FMOD_Channel_SetPaused(this.channel, false);
                    return false;
                }
                return false;
            }
            if (currentSquare.getRoom() == null) {
                if (!this.ambient) {
                    DistanceManhatten += IsoPlayer.getInstance().numNearbyBuildingsRooms / 32.0f;
                }
                if (!this.ambient) {
                    DistanceManhatten += 0.08f;
                }
            } else {
                float size = currentSquare.getRoom().Squares.size();
                if (!this.ambient) {
                    DistanceManhatten += size / 500.0f;
                }
            }
            if (DistanceManhatten > 1.0f) {
                DistanceManhatten = 1.0f;
            }
            float f6 = DistanceManhatten * DistanceManhatten;
            float f7 = f6 * f6 * this.clip.reverbFactor * 10.0f;
            if (IsoPlayer.getInstance().getCurrentSquare().getRoom() != null || f7 < 0.1f) {
            }
            if (!this.ambient) {
                if (currentSquare.getRoom() != null) {
                    i = 0;
                    i2 = 1;
                    i3 = 2;
                } else {
                    i = 2;
                    i2 = 0;
                    i3 = 1;
                }
            } else {
                i = 2;
                i2 = 0;
                i3 = 1;
            }
            IsoGridSquare gridSquare2 = IsoWorld.instance.CurrentCell.getGridSquare(f2, f3, f4);
            if (gridSquare2 != null && gridSquare2.getZone() != null && (gridSquare2.getZone().getType().equals("Forest") || gridSquare2.getZone().getType().equals("DeepForest"))) {
                i = 1;
                i2 = 0;
                i3 = 2;
            }
            javafmod.FMOD_Channel_SetReverbProperties(this.channel, i, 0.0f);
            javafmod.FMOD_Channel_SetReverbProperties(this.channel, i2, 0.0f);
            javafmod.FMOD_Channel_SetReverbProperties(this.channel, i3, 0.0f);
            javafmod.FMOD_Channel_Set3DMinMaxDistance(this.channel, f, this.clip.distanceMax);
            IsoGridSquare gridSquare3 = IsoWorld.instance.CurrentCell.getGridSquare(f2, f3, f4);
            float f8 = 0.0f;
            float f9 = 0.0f;
            if (gridSquare3 != null) {
                if ((this.emitter.parent instanceof IsoWindow) || (this.emitter.parent instanceof IsoDoor)) {
                    IsoRoom room2 = IsoPlayer.getInstance().getCurrentSquare().getRoom();
                    if (room2 != this.emitter.parent.square.getRoom()) {
                        if (room2 != null && room2.getBuilding() == this.emitter.parent.square.getBuilding()) {
                            f8 = 0.33f;
                            f9 = 0.33f;
                        } else {
                            if (this.emitter.parent instanceof IsoDoor) {
                                if (this.emitter.parent.north) {
                                    gridSquare = IsoWorld.instance.CurrentCell.getGridSquare(r0.getX(), r0.getY() - 1.0f, r0.getZ());
                                } else {
                                    gridSquare = IsoWorld.instance.CurrentCell.getGridSquare(r0.getX() - 1.0f, r0.getY(), r0.getZ());
                                }
                            } else {
                                if (this.emitter.parent.north) {
                                    gridSquare = IsoWorld.instance.CurrentCell.getGridSquare(r0.getX(), r0.getY() - 1.0f, r0.getZ());
                                } else {
                                    gridSquare = IsoWorld.instance.CurrentCell.getGridSquare(r0.getX() - 1.0f, r0.getY(), r0.getZ());
                                }
                            }
                            if (gridSquare != null && ((room = IsoPlayer.getInstance().getCurrentSquare().getRoom()) != null || gridSquare.getRoom() == null)) {
                                if (room != null && gridSquare.getRoom() != null && room.building == gridSquare.getBuilding()) {
                                    if (room != gridSquare.getRoom()) {
                                        if (room.def.level == gridSquare.getZ()) {
                                            f8 = 0.33f;
                                            f9 = 0.33f;
                                        } else {
                                            f8 = 0.6f;
                                            f9 = 0.6f;
                                        }
                                    }
                                } else {
                                    f8 = 0.33f;
                                    f9 = 0.33f;
                                }
                            }
                        }
                    }
                } else if (gridSquare3.getRoom() != null) {
                    IsoRoom room3 = IsoPlayer.getInstance().getCurrentSquare().getRoom();
                    if (room3 == null) {
                        f8 = 0.33f;
                        f9 = 0.23f;
                    } else if (room3 != gridSquare3.getRoom()) {
                        f8 = 0.24f;
                        f9 = 0.24f;
                    }
                    if (room3 != null && gridSquare3.getRoom().getBuilding() != room3.getBuilding()) {
                        f8 = 1.0f;
                        f9 = 0.8f;
                    }
                    if (room3 != null && gridSquare3.getRoom().def.level != ((int) IsoPlayer.getInstance().z)) {
                        f8 = 0.6f;
                        f9 = 0.6f;
                    }
                } else if (IsoPlayer.getInstance().getCurrentSquare().getRoom() != null) {
                    f8 = 0.79f;
                    f9 = 0.59f;
                }
                if (!gridSquare3.isCouldSee(IsoPlayer.getPlayerIndex()) && gridSquare3 != IsoPlayer.getInstance().getCurrentSquare()) {
                    f8 += 0.4f;
                }
            } else {
                if (IsoWorld.instance.MetaGrid.getRoomAt((int) f2, (int) f3, (int) f4) != null) {
                    f8 = 1.0f;
                    f9 = 1.0f;
                }
                if (IsoPlayer.getInstance().getCurrentSquare().getRoom() != null) {
                    f8 += 0.94f;
                } else {
                    f8 += 0.6f;
                }
            }
            if (gridSquare3 != null && ((int) IsoPlayer.getInstance().z) != gridSquare3.getZ()) {
                f8 *= 1.3f;
            }
            if (f8 > 0.9f) {
                f8 = 0.9f;
            }
            if (f9 > 0.9f) {
                f9 = 0.9f;
            }
            if (this.emitter.emitterType == EmitterType.Footstep && f4 > IsoPlayer.getInstance().z && gridSquare3.getBuilding() == IsoPlayer.getInstance().getBuilding()) {
                f8 = 0.0f;
                f9 = 0.0f;
            }
            if ("HouseAlarm".equals(this.name)) {
                f8 = 0.0f;
                f9 = 0.0f;
            }
            javafmod.FMOD_Channel_Set3DOcclusion(this.channel, f8, f9);
            javafmod.FMOD_Channel_SetVolume(this.channel, getVolume());
            javafmod.FMOD_Channel_SetPitch(this.channel, this.pitch);
            if (z) {
                javafmod.FMOD_Channel_SetPaused(this.channel, false);
            }
            this.lx = f2;
            this.ly = f3;
            this.lz = f4;
            return false;
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        void setParameterValue(FMOD_STUDIO_PARAMETER_DESCRIPTION fmod_studio_parameter_description, float f) {
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        void setTimelinePosition(String str) {
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        void triggerCue() {
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        boolean isTriggeredCue() {
            return false;
        }

        @Override // fmod.fmod.FMODSoundEmitter.Sound
        boolean restart() {
            return false;
        }
    }

    /* loaded from: FMODSoundEmitter$ParameterValue.class */
    private static final class ParameterValue {
        long eventInstance;
        FMOD_STUDIO_PARAMETER_DESCRIPTION parameterDescription;
        float value;

        private ParameterValue() {
        }
    }

    public FMODSoundEmitter() {
        SoundManager.instance.registerEmitter(this);
        if (parameterSet == null) {
            parameterSet = new BitSet(FMODManager.instance.getParameterCount());
        }
    }

    public void randomStart() {
    }

    public void setPos(float f, float f2, float f3) {
        this.x = f;
        this.y = f2;
        this.z = f3;
    }

    public int stopSound(long j) {
        int i = 0;
        while (i < this.ToStart.size()) {
            Sound sound = this.ToStart.get(i);
            if (sound.getRef() == j) {
                sendStopSound(sound.name, false);
                sound.release();
                int i2 = i;
                i--;
                this.ToStart.remove(i2);
            }
            i++;
        }
        int i3 = 0;
        while (i3 < this.Instances.size()) {
            Sound sound2 = this.Instances.get(i3);
            if (sound2.getRef() == j) {
                sound2.stop();
                sendStopSound(sound2.name, false);
                sound2.release();
                int i4 = i3;
                i3--;
                this.Instances.remove(i4);
            }
            i3++;
        }
        return 0;
    }

    public void stopSoundLocal(long j) {
        int i = 0;
        while (i < this.ToStart.size()) {
            Sound sound = this.ToStart.get(i);
            if (sound.getRef() == j) {
                sound.release();
                int i2 = i;
                i--;
                this.ToStart.remove(i2);
            }
            i++;
        }
        int i3 = 0;
        while (i3 < this.Instances.size()) {
            Sound sound2 = this.Instances.get(i3);
            if (sound2.getRef() == j) {
                sound2.stop();
                sound2.release();
                int i4 = i3;
                i3--;
                this.Instances.remove(i4);
            }
            i3++;
        }
    }

    public int stopSoundByName(String str) {
        GameSound sound = GameSounds.getSound(str);
        if (sound == null) {
            return 0;
        }
        int i = 0;
        while (i < this.ToStart.size()) {
            Sound sound2 = this.ToStart.get(i);
            if (sound.clips.contains(sound2.clip)) {
                sound2.release();
                int i2 = i;
                i--;
                this.ToStart.remove(i2);
            }
            i++;
        }
        int i3 = 0;
        while (i3 < this.Instances.size()) {
            Sound sound3 = this.Instances.get(i3);
            if (sound.clips.contains(sound3.clip)) {
                sound3.stop();
                sound3.release();
                int i4 = i3;
                i3--;
                this.Instances.remove(i4);
            }
            i3++;
        }
        return 0;
    }

    public void stopOrTriggerSound(long j) {
        int findToStart = findToStart(j);
        if (findToStart != -1) {
            Sound remove = this.ToStart.remove(findToStart);
            sendStopSound(remove.name, true);
            remove.release();
            return;
        }
        int findInstance = findInstance(j);
        if (findInstance != -1) {
            Sound sound = this.Instances.get(findInstance);
            sendStopSound(sound.name, true);
            if (sound.clip.hasSustainPoints()) {
                sound.triggerCue();
                return;
            }
            this.Instances.remove(findInstance);
            sound.stop();
            sound.release();
        }
    }

    public void stopOrTriggerSoundByName(String str) {
        GameSound sound = GameSounds.getSound(str);
        if (sound == null) {
            return;
        }
        int i = 0;
        while (i < this.ToStart.size()) {
            Sound sound2 = this.ToStart.get(i);
            if (sound.clips.contains(sound2.clip)) {
                int i2 = i;
                i--;
                this.ToStart.remove(i2);
                sound2.release();
            }
            i++;
        }
        int i3 = 0;
        while (i3 < this.Instances.size()) {
            Sound sound3 = this.Instances.get(i3);
            if (sound.clips.contains(sound3.clip)) {
                if (sound3.clip.hasSustainPoints()) {
                    sound3.triggerCue();
                } else {
                    sound3.stop();
                    sound3.release();
                    int i4 = i3;
                    i3--;
                    this.Instances.remove(i4);
                }
            }
            i3++;
        }
    }

    private void limitSound(GameSound gameSound, int i) {
        int countToStart = countToStart(gameSound) + countInstances(gameSound);
        if (countToStart <= i) {
            return;
        }
        int i2 = 0;
        while (i2 < this.ToStart.size()) {
            Sound sound = this.ToStart.get(i2);
            if (gameSound.clips.contains(sound.clip)) {
                int i3 = i2;
                i2--;
                this.ToStart.remove(i3);
                sound.release();
                countToStart--;
                if (countToStart <= i) {
                    return;
                }
            }
            i2++;
        }
        int i4 = 0;
        while (i4 < this.Instances.size()) {
            Sound sound2 = this.Instances.get(i4);
            if (gameSound.clips.contains(sound2.clip)) {
                if (sound2.clip.hasSustainPoints()) {
                    if (!sound2.isTriggeredCue()) {
                        sound2.triggerCue();
                    }
                } else {
                    sound2.stop();
                    sound2.release();
                    int i5 = i4;
                    i4--;
                    this.Instances.remove(i5);
                    countToStart--;
                    if (countToStart <= i) {
                        return;
                    }
                }
            }
            i4++;
        }
    }

    public void setVolume(long j, float f) {
        for (int i = 0; i < this.ToStart.size(); i++) {
            Sound sound = this.ToStart.get(i);
            if (sound.getRef() == j) {
                sound.volume = f;
            }
        }
        for (int i2 = 0; i2 < this.Instances.size(); i2++) {
            Sound sound2 = this.Instances.get(i2);
            if (sound2.getRef() == j) {
                sound2.volume = f;
            }
        }
    }

    public void setPitch(long j, float f) {
        for (int i = 0; i < this.ToStart.size(); i++) {
            Sound sound = this.ToStart.get(i);
            if (sound.getRef() == j) {
                sound.pitch = f;
            }
        }
        for (int i2 = 0; i2 < this.Instances.size(); i2++) {
            Sound sound2 = this.Instances.get(i2);
            if (sound2.getRef() == j) {
                sound2.pitch = f;
            }
        }
    }

    public boolean hasSustainPoints(long j) {
        for (int i = 0; i < this.ToStart.size(); i++) {
            Sound sound = this.ToStart.get(i);
            if (sound.getRef() == j) {
                if (sound.clip.eventDescription == null) {
                    return false;
                }
                return sound.clip.eventDescription.bHasSustainPoints;
            }
        }
        for (int i2 = 0; i2 < this.Instances.size(); i2++) {
            Sound sound2 = this.Instances.get(i2);
            if (sound2.getRef() == j) {
                if (sound2.clip.eventDescription == null) {
                    return false;
                }
                return sound2.clip.eventDescription.bHasSustainPoints;
            }
        }
        return false;
    }

    public void triggerCue(long j) {
        for (int i = 0; i < this.Instances.size(); i++) {
            Sound sound = this.Instances.get(i);
            if (sound.getRef() == j) {
                sound.triggerCue();
            }
        }
    }

    public void setVolumeAll(float f) {
        for (int i = 0; i < this.ToStart.size(); i++) {
            this.ToStart.get(i).volume = f;
        }
        for (int i2 = 0; i2 < this.Instances.size(); i2++) {
            this.Instances.get(i2).volume = f;
        }
    }

    public void stopAll() {
        for (int i = 0; i < this.ToStart.size(); i++) {
            this.ToStart.get(i).release();
        }
        for (int i2 = 0; i2 < this.Instances.size(); i2++) {
            Sound sound = this.Instances.get(i2);
            sound.stop();
            sound.release();
        }
        this.ToStart.clear();
        this.Instances.clear();
    }

    public long playSound(String str) {
        if (GameClient.bClient) {
            if (this.parent instanceof IsoMovingObject) {
                if (!(this.parent instanceof IsoPlayer) || !this.parent.isInvisible()) {
                    GameClient.instance.PlaySound(str, false, this.parent);
                }
            } else {
                GameClient.instance.PlayWorldSound(str, (int) this.x, (int) this.y, (byte) this.z);
            }
        }
        if (GameServer.bServer) {
            return 0L;
        }
        return playSoundImpl(str, (IsoObject) null);
    }

    public long playSound(String str, IsoGameCharacter isoGameCharacter) {
        if (GameClient.bClient) {
            if (!isoGameCharacter.isInvisible()) {
                GameClient.instance.PlaySound(str, false, isoGameCharacter);
            }
            if (!isoGameCharacter.isInvisible() || DebugOptions.instance.Character.Debug.PlaySoundWhenInvisible.getValue()) {
                return playSoundImpl(str, (IsoObject) null);
            }
            return 0L;
        }
        if (GameServer.bServer) {
            return 0L;
        }
        return playSoundImpl(str, (IsoObject) null);
    }

    public long playSound(String str, int i, int i2, int i3) {
        this.x = i;
        this.y = i2;
        this.z = i3;
        return playSound(str);
    }

    public long playSound(String str, IsoGridSquare isoGridSquare) {
        this.x = isoGridSquare.x + 0.5f;
        this.y = isoGridSquare.y + 0.5f;
        this.z = isoGridSquare.z;
        return playSound(str);
    }

    public long playSoundImpl(String str, IsoGridSquare isoGridSquare) {
        this.x = isoGridSquare.x + 0.5f;
        this.y = isoGridSquare.y + 0.5f;
        this.z = isoGridSquare.z + 0.5f;
        return playSoundImpl(str, (IsoObject) null);
    }

    public long playSound(String str, boolean z) {
        return playSound(str);
    }

    public long playSoundImpl(String str, boolean z, IsoObject isoObject) {
        return playSoundImpl(str, isoObject);
    }

    public long playSoundLooped(String str) {
        if (GameClient.bClient) {
            if (this.parent instanceof IsoMovingObject) {
                GameClient.instance.PlaySound(str, true, this.parent);
            } else {
                GameClient.instance.PlayWorldSound(str, (int) this.x, (int) this.y, (byte) this.z);
            }
        }
        return playSoundLoopedImpl(str);
    }

    public long playSoundLoopedImpl(String str) {
        return playSoundImpl(str, false, null);
    }

    public long playSound(String str, IsoObject isoObject) {
        if (GameClient.bClient) {
            if (isoObject instanceof IsoMovingObject) {
                GameClient.instance.PlaySound(str, false, this.parent);
            } else {
                GameClient.instance.PlayWorldSound(str, (int) this.x, (int) this.y, (byte) this.z);
            }
        }
        if (GameServer.bServer) {
            return 0L;
        }
        return playSoundImpl(str, isoObject);
    }

    public long playSoundImpl(String str, IsoObject isoObject) {
        GameSound sound = GameSounds.getSound(str);
        if (sound == null) {
            return 0L;
        }
        return playClip(sound.getRandomClip(), isoObject);
    }

    public long playClip(GameSoundClip gameSoundClip, IsoObject isoObject) {
        Sound addSound = addSound(gameSoundClip, 1.0f, isoObject);
        if (addSound == null) {
            return 0L;
        }
        return addSound.getRef();
    }

    public long playAmbientSound(String str) {
        GameSound sound;
        if (GameServer.bServer || (sound = GameSounds.getSound(str)) == null) {
            return 0L;
        }
        Sound addSound = addSound(sound.getRandomClip(), 1.0f, null);
        if (addSound instanceof FileSound) {
            ((FileSound) addSound).ambient = true;
        }
        if (addSound == null) {
            return 0L;
        }
        return addSound.getRef();
    }

    public long playAmbientLoopedImpl(String str) {
        GameSound sound;
        Sound addSound;
        if (GameServer.bServer || (sound = GameSounds.getSound(str)) == null || (addSound = addSound(sound.getRandomClip(), 1.0f, null)) == null) {
            return 0L;
        }
        return addSound.getRef();
    }

    public void set3D(long j, boolean z) {
        for (int i = 0; i < this.ToStart.size(); i++) {
            Sound sound = this.ToStart.get(i);
            if (sound.getRef() == j) {
                sound.set3D(z);
            }
        }
        for (int i2 = 0; i2 < this.Instances.size(); i2++) {
            Sound sound2 = this.Instances.get(i2);
            if (sound2.getRef() == j) {
                sound2.set3D(z);
            }
        }
    }

    public void tick() {
        if (!isEmpty()) {
            this.occlusion.update();
            for (int i = 0; i < this.parameters.size(); i++) {
                this.parameters.get(i).update();
            }
        }
        for (int i2 = 0; i2 < this.ToStart.size(); i2++) {
            this.Instances.add(this.ToStart.get(i2));
        }
        int i3 = 0;
        while (i3 < this.Instances.size()) {
            Sound sound = this.Instances.get(i3);
            if (sound.tick(this.ToStart.contains(sound))) {
                int i4 = i3;
                i3--;
                this.Instances.remove(i4);
                sound.release();
            }
            i3++;
        }
        this.ToStart.clear();
    }

    public boolean hasSoundsToStart() {
        return !this.ToStart.isEmpty();
    }

    public boolean isEmpty() {
        return this.ToStart.isEmpty() && this.Instances.isEmpty();
    }

    public boolean isPlaying(long j) {
        for (int i = 0; i < this.ToStart.size(); i++) {
            if (this.ToStart.get(i).getRef() == j) {
                return true;
            }
        }
        for (int i2 = 0; i2 < this.Instances.size(); i2++) {
            if (this.Instances.get(i2).getRef() == j) {
                return true;
            }
        }
        return false;
    }

    public boolean isPlaying(String str) {
        for (int i = 0; i < this.ToStart.size(); i++) {
            if (str.equals(this.ToStart.get(i).name)) {
                return true;
            }
        }
        for (int i2 = 0; i2 < this.Instances.size(); i2++) {
            if (str.equals(this.Instances.get(i2).name)) {
                return true;
            }
        }
        return false;
    }

    public boolean restart(long j) {
        if (findToStart(j) != -1) {
            return true;
        }
        int findInstance = findInstance(j);
        if (findInstance != -1 && this.Instances.get(findInstance).restart()) {
            return true;
        }
        return false;
    }

    private int findInstance(long j) {
        for (int i = 0; i < this.Instances.size(); i++) {
            if (this.Instances.get(i).getRef() == j) {
                return i;
            }
        }
        return -1;
    }

    private int findInstance(String str) {
        GameSound sound = GameSounds.getSound(str);
        if (sound == null) {
            return -1;
        }
        for (int i = 0; i < this.Instances.size(); i++) {
            if (sound.clips.contains(this.Instances.get(i).clip)) {
                return i;
            }
        }
        return -1;
    }

    private int findToStart(long j) {
        for (int i = 0; i < this.ToStart.size(); i++) {
            if (this.ToStart.get(i).getRef() == j) {
                return i;
            }
        }
        return -1;
    }

    private int findToStart(String str) {
        GameSound sound = GameSounds.getSound(str);
        if (sound == null) {
            return -1;
        }
        for (int i = 0; i < this.ToStart.size(); i++) {
            if (sound.clips.contains(this.ToStart.get(i).clip)) {
                return i;
            }
        }
        return -1;
    }

    private int countToStart(GameSound gameSound) {
        int i = 0;
        for (int i2 = 0; i2 < this.ToStart.size(); i2++) {
            if (gameSound.clips.contains(this.ToStart.get(i2).clip)) {
                i++;
            }
        }
        return i;
    }

    private int countInstances(GameSound gameSound) {
        int i = 0;
        for (int i2 = 0; i2 < this.Instances.size(); i2++) {
            if (gameSound.clips.contains(this.Instances.get(i2).clip)) {
                i++;
            }
        }
        return i;
    }

    public void addParameter(FMODParameter fMODParameter) {
        this.parameters.add(fMODParameter);
    }

    public void setParameterValue(long j, FMOD_STUDIO_PARAMETER_DESCRIPTION fmod_studio_parameter_description, float f) {
        if (j == 0 || fmod_studio_parameter_description == null) {
            return;
        }
        int findInstance = findInstance(j);
        if (findInstance != -1) {
            this.Instances.get(findInstance).setParameterValue(fmod_studio_parameter_description, f);
            return;
        }
        int findParameterValue = findParameterValue(j, fmod_studio_parameter_description);
        if (findParameterValue != -1) {
            this.parameterValues.get(findParameterValue).value = f;
            return;
        }
        ParameterValue parameterValue = (ParameterValue) parameterValuePool.alloc();
        parameterValue.eventInstance = j;
        parameterValue.parameterDescription = fmod_studio_parameter_description;
        parameterValue.value = f;
        this.parameterValues.add(parameterValue);
    }

    public void setTimelinePosition(long j, String str) {
        int findToStart;
        if (j != 0 && (findToStart = findToStart(j)) != -1) {
            this.ToStart.get(findToStart).setTimelinePosition(str);
        }
    }

    private int findParameterValue(long j, FMOD_STUDIO_PARAMETER_DESCRIPTION fmod_studio_parameter_description) {
        for (int i = 0; i < this.parameterValues.size(); i++) {
            ParameterValue parameterValue = this.parameterValues.get(i);
            if (parameterValue.eventInstance == j && parameterValue.parameterDescription == fmod_studio_parameter_description) {
                return i;
            }
        }
        return -1;
    }

    public void clearParameters() {
        this.occlusion.resetToDefault();
        this.parameters.clear();
        parameterValuePool.releaseAll(this.parameterValues);
        this.parameterValues.clear();
    }

    private void startEvent(long j, GameSoundClip gameSoundClip) {
        parameterSet.clear();
        ArrayList<FMODParameter> arrayList = this.parameters;
        ArrayList<FMOD_STUDIO_PARAMETER_DESCRIPTION> arrayList2 = gameSoundClip.eventDescription.parameters;
        for (int i = 0; i < arrayList2.size(); i++) {
            FMOD_STUDIO_PARAMETER_DESCRIPTION fmod_studio_parameter_description = arrayList2.get(i);
            int findParameterValue = findParameterValue(j, fmod_studio_parameter_description);
            if (findParameterValue != -1) {
                javafmod.FMOD_Studio_EventInstance_SetParameterByID(j, fmod_studio_parameter_description.id, this.parameterValues.get(findParameterValue).value, false);
                parameterSet.set(fmod_studio_parameter_description.globalIndex, true);
            } else if (fmod_studio_parameter_description == this.occlusion.getParameterDescription()) {
                this.occlusion.startEventInstance(j);
                parameterSet.set(fmod_studio_parameter_description.globalIndex, true);
            } else {
                int i2 = 0;
                while (true) {
                    if (i2 < arrayList.size()) {
                        FMODParameter fMODParameter = arrayList.get(i2);
                        if (fMODParameter.getParameterDescription() != fmod_studio_parameter_description) {
                            i2++;
                        } else {
                            fMODParameter.startEventInstance(j);
                            parameterSet.set(fmod_studio_parameter_description.globalIndex, true);
                            break;
                        }
                    }
                }
            }
        }
        if (this.parameterUpdater != null) {
            this.parameterUpdater.startEvent(j, gameSoundClip, parameterSet);
        }
    }

    private void updateEvent(long j, GameSoundClip gameSoundClip) {
        if (this.parameterUpdater != null) {
            this.parameterUpdater.updateEvent(j, gameSoundClip);
        }
    }

    private void stopEvent(long j, GameSoundClip gameSoundClip) {
        parameterSet.clear();
        ArrayList<FMODParameter> arrayList = this.parameters;
        ArrayList<FMOD_STUDIO_PARAMETER_DESCRIPTION> arrayList2 = gameSoundClip.eventDescription.parameters;
        for (int i = 0; i < arrayList2.size(); i++) {
            FMOD_STUDIO_PARAMETER_DESCRIPTION fmod_studio_parameter_description = arrayList2.get(i);
            int findParameterValue = findParameterValue(j, fmod_studio_parameter_description);
            if (findParameterValue != -1) {
                parameterValuePool.release(this.parameterValues.remove(findParameterValue));
                parameterSet.set(fmod_studio_parameter_description.globalIndex, true);
            } else if (fmod_studio_parameter_description == this.occlusion.getParameterDescription()) {
                this.occlusion.stopEventInstance(j);
                parameterSet.set(fmod_studio_parameter_description.globalIndex, true);
            } else {
                int i2 = 0;
                while (true) {
                    if (i2 < arrayList.size()) {
                        FMODParameter fMODParameter = arrayList.get(i2);
                        if (fMODParameter.getParameterDescription() != fmod_studio_parameter_description) {
                            i2++;
                        } else {
                            fMODParameter.stopEventInstance(j);
                            parameterSet.set(fmod_studio_parameter_description.globalIndex, true);
                            break;
                        }
                    }
                }
            }
        }
        if (this.parameterUpdater != null) {
            this.parameterUpdater.stopEvent(j, gameSoundClip, parameterSet);
        }
    }

    private EventSound allocEventSound() {
        return this.eventSoundPool.isEmpty() ? new EventSound(this) : this.eventSoundPool.pop();
    }

    private void releaseEventSound(EventSound eventSound) {
        if (!$assertionsDisabled && this.eventSoundPool.contains(eventSound)) {
            throw new AssertionError();
        }
        this.eventSoundPool.push(eventSound);
    }

    private FileSound allocFileSound() {
        return this.fileSoundPool.isEmpty() ? new FileSound(this) : this.fileSoundPool.pop();
    }

    private void releaseFileSound(FileSound fileSound) {
        if (!$assertionsDisabled && this.fileSoundPool.contains(fileSound)) {
            throw new AssertionError();
        }
        this.fileSoundPool.push(fileSound);
    }

    private Sound addSound(GameSoundClip gameSoundClip, float f, IsoObject isoObject) {
        if (gameSoundClip == null) {
            DebugLog.log("null sound passed to SoundEmitter.playSoundImpl");
            return null;
        }
        if (gameSoundClip.gameSound.maxInstancesPerEmitter > 0) {
            limitSound(gameSoundClip.gameSound, gameSoundClip.gameSound.maxInstancesPerEmitter - 1);
        }
        if (gameSoundClip.event != null && !gameSoundClip.event.isEmpty()) {
            if (gameSoundClip.eventDescription == null) {
                return null;
            }
            FMOD_STUDIO_EVENT_DESCRIPTION fmod_studio_event_description = gameSoundClip.eventDescription;
            if (gameSoundClip.eventDescriptionMP != null && (this.parent instanceof IsoPlayer) && !this.parent.isLocalPlayer()) {
                fmod_studio_event_description = gameSoundClip.eventDescriptionMP;
            }
            long FMOD_Studio_System_CreateEventInstance = javafmod.FMOD_Studio_System_CreateEventInstance(fmod_studio_event_description.address);
            if (FMOD_Studio_System_CreateEventInstance < 0) {
                return null;
            }
            if (gameSoundClip.hasMinDistance()) {
                javafmodJNI.FMOD_Studio_EventInstance_SetProperty(FMOD_Studio_System_CreateEventInstance, FMOD_STUDIO_EVENT_PROPERTY.FMOD_STUDIO_EVENT_PROPERTY_MINIMUM_DISTANCE.ordinal(), gameSoundClip.getMinDistance());
            }
            if (gameSoundClip.hasMaxDistance()) {
                javafmodJNI.FMOD_Studio_EventInstance_SetProperty(FMOD_Studio_System_CreateEventInstance, FMOD_STUDIO_EVENT_PROPERTY.FMOD_STUDIO_EVENT_PROPERTY_MAXIMUM_DISTANCE.ordinal(), gameSoundClip.getMaxDistance());
            }
            EventSound allocEventSound = allocEventSound();
            allocEventSound.clip = gameSoundClip;
            allocEventSound.name = gameSoundClip.gameSound.getName();
            allocEventSound.eventInstance = FMOD_Studio_System_CreateEventInstance;
            allocEventSound.volume = f;
            allocEventSound.parent = isoObject;
            allocEventSound.setVolume = 1.0f;
            allocEventSound.setZ = 0.0f;
            allocEventSound.setY = 0.0f;
            allocEventSound.setX = 0.0f;
            this.ToStart.add(allocEventSound);
            return allocEventSound;
        }
        if (gameSoundClip.file != null && !gameSoundClip.file.isEmpty()) {
            long loadSound = FMODManager.instance.loadSound(gameSoundClip.file);
            if (loadSound == 0) {
                return null;
            }
            long FMOD_System_PlaySound = javafmod.FMOD_System_PlaySound(loadSound, true);
            javafmod.FMOD_Channel_SetVolume(FMOD_System_PlaySound, 0.0f);
            javafmod.FMOD_Channel_SetPriority(FMOD_System_PlaySound, 9 - gameSoundClip.priority);
            javafmod.FMOD_Channel_SetChannelGroup(FMOD_System_PlaySound, FMODManager.instance.channelGroupInGameNonBankSounds);
            if (gameSoundClip.distanceMax == 0.0f || (this.x == 0.0f && this.y == 0.0f)) {
                javafmod.FMOD_Channel_SetMode(FMOD_System_PlaySound, FMODManager.FMOD_2D);
            }
            FileSound allocFileSound = allocFileSound();
            allocFileSound.clip = gameSoundClip;
            allocFileSound.name = gameSoundClip.gameSound.getName();
            allocFileSound.sound = loadSound;
            allocFileSound.pitch = gameSoundClip.pitch;
            allocFileSound.channel = FMOD_System_PlaySound;
            allocFileSound.parent = isoObject;
            allocFileSound.volume = f;
            allocFileSound.setVolume = 1.0f;
            allocFileSound.setZ = 0.0f;
            allocFileSound.setY = 0.0f;
            allocFileSound.setX = 0.0f;
            allocFileSound.is3D = (byte) -1;
            allocFileSound.ambient = false;
            this.ToStart.add(allocFileSound);
            return allocFileSound;
        }
        return null;
    }

    private void sendStopSound(String str, boolean z) {
        if (GameClient.bClient && (this.parent instanceof IsoMovingObject)) {
            GameClient.instance.StopSound(this.parent, str, z);
        }
    }

    public static void update() {
        CurrentTimeMS = System.currentTimeMillis();
    }
}